﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bag_Panel : MonoBehaviour
{
    GameObject itemPrefab;
    public int ItemCount;
    UIGrid grid;
    List<BagItem> items = new List<BagItem>();
    // Start is called before the first frame update
    void Start()
    {
        //从资源文件调用Item并转为GameObject类型
        itemPrefab = Resources.Load("Item") as GameObject;
        //找到UIGrid组件
        grid = transform.Find("Scroll View/Grid").GetComponent<UIGrid>();
        //实例化Item预制体，并添加BagItem组件
        for (int i = 0; i < ItemCount; i++)
        {
            GameObject go = Instantiate(itemPrefab, grid.transform);
            items.Add(go.GetComponent<BagItem>());
            grid.Reposition();
        }
        items[5].Init(WeaponFactory.Instance.GetWeaponByID(1));
       // items[5].Refresh();
       //for (int i = 0; i < ItemCount; i++)
       //{
       //    items[i].Init(WeaponFactory.Instance.GetWeaponByID(2));
       //    items[i].Refresh();
       //    
       //}
    }
    void Refresh()
    {
        for (int i = 0; i < UserData.Instance.ItemCount; i++)
        {
            Weapon _weapon;
            if (UserData.Instance.weapons.TryGetValue(i,out _weapon))
            {
                items[i].Init(_weapon);
            }
            else
            {
                items[i].Clean();
            }
            items[i].Refresh();
        }
    }
    
    void Update()
    {
        
    }
}
